package salaryService;

import salaryEntity.emIfoEntity;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import static salaryDao.employeeDao.addDataEmployeeInfo;
import static salaryDao.employeeDao.getEmployeeByName;

public class registerDeal extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        emIfoEntity employee = new emIfoEntity();
        String name = (String) req.getParameter("employeename");
        String ID = req.getParameter("employeeID");
        String password = req.getParameter("password");
        String sex = (String) req.getParameter("sex");
        String birthDay = req.getParameter("birthDay");
        String phone = req.getParameter("phone");
        String Email = req.getParameter("Email");

            employee.setEmployeename(name);
            employee.setEmployeeID(ID);
            employee.setPassword(password);
            employee.setSex(sex);
            employee.setBirthDay(birthDay);
            employee.setPhone(phone);
            employee.setEmail(Email);
//  调用 getEmployeeByName(name)方法验证用户名是否已经存在
            emIfoEntity returnValues = getEmployeeByName(name);
            if (returnValues != null) {
//        如果不为空，则证明用户已经存在,请进行登录
//        调用employeeDao的数据添加方法，将实体对象传给addDataEmployeeInfo(employee)
                req.setAttribute("register_msg", "员工名已经存在，请重新注册！");
                req.getRequestDispatcher("register.jsp").forward(req, resp);
            }
            else {
                addDataEmployeeInfo(employee);
                resp.sendRedirect("login.jsp");
            }
        }

}
